//
//  StatusPictureVIew.swift
//  weibo
//
//  Created by 谭仁川 on 15/12/2.
//  Copyright © 2015年 itcast. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

private let pictureCellID = "pictureCellID"
private let pictureCellMargin: CGFloat = 5

class StatusPictureVIew: UICollectionView {
    
    var imageURLs: [NSURL]?{
        
        didSet {
            if imageURLs != nil {
            
                testLabel.text = "\(imageURLs!.count)"
            }
            let pSize = caclePictureViewSize()
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(pSize)
            }
            reloadData()
        }
    }
    private func caclePictureViewSize() -> CGSize {
    
        let imageCount = imageURLs?.count ?? 0
        let maxWidth = screenW - 2 * StatusCellMargin
        let itemWidth = (maxWidth - 2 * pictureCellMargin) / 3
        let layout = collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = CGSizeMake(itemWidth, itemWidth)
        
        if imageCount == 0 {
            return CGSizeZero
        }
        
        if imageCount == 1 {
            //TODO: 先显示固定大小
            let imageSize = CGSizeMake(180, 120)
            layout.itemSize = imageSize
            return imageSize
        }
        
        if imageCount == 4 {
            let pW = itemWidth * 2 + pictureCellMargin
            return CGSizeMake(pW, pW)
        }
        let row = CGFloat((imageCount - 1) / 3 + 1)
        
        return  CGSizeMake(maxWidth, itemWidth * row + pictureCellMargin * (row - 1))
    }
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = pictureCellMargin
        layout.minimumLineSpacing = pictureCellMargin
        
        super.init(frame: CGRectZero, collectionViewLayout: layout)
        //注册cell
        registerClass(PictureCell.self, forCellWithReuseIdentifier: pictureCellID)
        
        dataSource = self
        backgroundColor = UIColor.randomColor()
    
        setupUI()
    }
    
    private func setupUI(){
    
        addSubview(testLabel)
        testLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self.center)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private lazy var testLabel: UILabel = UILabel(title: "", color: UIColor.redColor(), fontSize: 30, margin: 0)

}

extension StatusPictureVIew: UICollectionViewDataSource {
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageURLs?.count ?? 0
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureCellID, forIndexPath: indexPath) as! PictureCell
        cell.imageURL = imageURLs![indexPath.item]
        
        return cell
    }
    
}
class PictureCell: UICollectionViewCell {
    var imageURL:NSURL? {
        didSet {
        iconView.sd_setImageWithURL(imageURL)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI (){
       contentView.addSubview(iconView)
        
        iconView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView.snp_edges)
        }
        
    }
    private lazy var iconView: UIImageView = UIImageView()
}